package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.Frame.AthChartJframe;
import org.come.Frame.ZhuFrame;
import org.come.bean.GoodsBean;
import org.come.bean.LaborRank;
import org.come.entity.Goodstable;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import com.tool.btn.TrueFeedbackBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;



/**
 * @author HGC<br>
 * @time 2020年4月28日 上午10:02:27<br>
 * @class 类名:TrueFeedbackLotteyJPanel 五一真情回馈抽奖面板<br>
 */
public class AthChartJPanel extends JPanel {
	private ImageIcon image3 = CutButtonImage.getImage("resource/NewUi/shanzi.gif",410,397);
    private TrueFeedbackBtn btnLottey;
    private JLabel[] labGoodsImg,djsimg;
    private JLabel labRank;
    private String[] msg;
    public AthChartJPanel() {
        setPreferredSize(new Dimension(430,450));
        setOpaque(false);
        setLayout(null);
        getBtnLottey();
        getLabGoodsImg();
        getLabRank();
    }

    /**展示面板*/
    public void showViewData(LaborRank laborRank){
    	Util.nums = 1;
        rank = laborRank.getRank();
        labRank.setText(String.valueOf(rank));
        String string = "";
        if(laborRank.getValue1()!=null && !"".equals(laborRank.getValue1())) {
	        String[] split1 = laborRank.getValue1().split("\\|");
	        msg = split1;
        	StringBuffer buffer=new StringBuffer();
        	for(int i=0;i<split1.length;i++) {
        		if (buffer.length()!=0) { 
        			buffer.append("|"); 
        		}
        		Goodstable goods = UserMessUntil.getGoodsBean().getgoods(new BigDecimal(split1[i]));
        		if(goods!=null) {
        			buffer.append(goods.getSkin());
        		}
        	}
        	string = buffer.toString();
        } else {
        	string = "484|484|484|484|484|484|484|484|484|484|484|484|484|484";
        	this.time3();
        }
        if(string!=null){
            String[] split = string.split("\\|");
            int length = labGoodsImg.length;
            for (int i = 0; i < split.length; i++) {
                if(length>i){
                    labGoodsImg[i].setIcon(GoodsListFromServerUntil.imgpathAdaptive(split[i], 53, 53));
                }
            }
        }
    }
    /**抽奖*/
    public void lotteyGoods(LaborRank laborRank){
        int count = 14-time;
        num = laborRank.getRank()+count;
        value = laborRank.getValue();
    }
    
    private ImageIcon downImg,lotteyImg;
    
    private int num, time,count,imgX=0,imgY=121,rank;
    private boolean is;
    private String value;
//    private Sprite tcp = SpriteFactory.loadSprite("resource/mouse/flicker.tcp", null);
    private Sprite cjwrk = SpriteFactory.VloadSprite("resource/mouse/cjwrk.tcp", null);
    
    /**闲置抽奖转动*/
//    public void grawInIdleLotteyImg(Graphics g){
//        count++;
//        if(count>(num>1?100/num:70)){
//            num--;
//            time++;
//            if(time>=14){
//                time=0;
//            }
//            count=0;
//            if(time<=4){
//                imgX = time*84;
//                imgY = 121;
//            }else if(time <=7){
//                imgX = 335;
//                imgY = 121+(time-4)*77;
//            }else if(time <=11){
//                imgX = 335-(time-7)*84;
//                imgY = 352;
//            }else if(time <=13){
//                imgX = 0;
//                imgY = 352-(time-11)*77;
//            }
//            if(num == 0){
//                 is= true; 
//            }
//        }
//        g.drawImage(lotteyImg.getImage(), imgX+20, imgY+20, null);
//    }

    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    /**抽奖时*/
    public void grawLotteyImg(Graphics g){
        if(num == 0){
            if(is){
                if(value!=null){
                	if(msg!=null && msg.length>0) {
                		for(int i=0;i<msg.length;i++) {
                			if(value.equals(i+"")) {
                				Goodstable goods = UserMessUntil.getGoodsBean().getgoods(new BigDecimal(msg[i]));
                				value = "恭喜您获得【"+goods.getGoodsname()+"】,真是运气爆棚啊！";
                				btnLottey.sendMes("14|"+goods.getGoodsid());

                                Runnable task = new Runnable() {
                                    private int count = 2;

                                    @Override
                                    public void run() {
                                        count--;
                                        if (count <= 0) {
                                            FormsManagement.HideForm(3001);
                                            Util.nums = 0;
                                            executorService.shutdown();
                                        }
                                    }
                                };

                                executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
                		        break;
                			}
                		}
                	}
            		ZhuFrame.getZhuJpanel().addPrompt2(value);
    				value=null; 
                }
                is = false;
            }
        }else{
            count++;
            if(count>(num>1?100/num:70)){
                num--;
                time++;
                if(time>=14){
                    time=0;
                }
                count=0;
                if(time<=4){
                    imgX = time*84;
                    imgY = 121;
                }else if(time <=7){
                    imgX = 335;
                    imgY = 121+(time-4)*77;
                }else if(time <=11){
                    imgX = 335-(time-7)*84;
                    imgY = 352;
                }else if(time <=13){
                    imgX = 0;
                    imgY = 352-(time-11)*77;
                }
                if(num == 0){
                     is= true; 
                }
            }
        }
        cjwrk.updateToTime(ImageMixDeal.userimg.getTime(), 0);
        cjwrk.draw(g, imgX, imgY);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
//        if(topImg == null){
//            topImg = new ImageIcon("inkImg/old/background/S198.png");
//        }   
//        g.drawImage(topImg.getImage(), 0, 0, null);
        if(downImg == null){
            downImg = new ImageIcon("inkImg/old/background/S1951.png");
        }   
        g.drawImage(downImg.getImage(), 0, 130, null);
        if(lotteyImg== null){
            lotteyImg = CutButtonImage.getImage("inkImg/old/background/S116.png", -1, -1);
        }
//        if(num!=null){
//            grawInIdleLotteyImg(g);
//        }else{
//        }
        g.drawImage(image3.getImage(), 195, 265, 42, 42, this);
    }
    @Override
    protected void paintChildren(Graphics g) {
        super.paintChildren(g);
        grawLotteyImg(g);
    }

    public boolean goCJPD() {
        if (this.getNum() != 0) {
            return false;
        }else {
        	return true;
        }
    }
    public TrueFeedbackBtn getBtnLottey() {
        if(btnLottey == null){
            btnLottey = new TrueFeedbackBtn("skin/8100226/newbutton/B21.png", 1, 100, "抽奖", UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, this);
            btnLottey.setBounds(165, 272, 79, 24);
            this.add(btnLottey);
        }
        return btnLottey;
    }

    public void setBtnLottey(TrueFeedbackBtn btnLottey) {
        this.btnLottey = btnLottey;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public JLabel[] getLabGoodsImg() {
        if(labGoodsImg == null){
            labGoodsImg = new JLabel[14];
            for (int i = 0; i < labGoodsImg.length; i++) {
                labGoodsImg[i] = new JLabel();
                if(i<=4){
                    if(i>=3) {
                        labGoodsImg[i].setBounds(187+(i-2)*84, 141, 53, 53);
                    } else {
                        labGoodsImg[i].setBounds(20+i*84, 141, 53, 53);
                    }
                }else if(i <=7){
                    labGoodsImg[i].setBounds(355, 141+(i-4)*77, 53, 53);
                }else if(i <=11){
                    if(i>=9) {
                        labGoodsImg[i].setBounds(355-(i-7)*84+1, 372, 53, 53);
                    } else {
                        labGoodsImg[i].setBounds(355-(i-7)*84, 372, 53, 53);
                    }
                }else if(i <=13){
                    labGoodsImg[i].setBounds(20, 373-(i-11)*77, 53, 53);
                }
                labGoodsImg[i].setOpaque(false);
                this.add(labGoodsImg[i]);
            }
        }
        return labGoodsImg;
    }

    public void setLabGoodsImg(JLabel[] labGoodsImg) {
        this.labGoodsImg = labGoodsImg;
    }

    public JLabel getLabRank() {
        if(labRank == null){
            labRank = new JLabel("100",SwingConstants.CENTER);
            labRank.setBounds(233, 318, 40, 14);
            labRank.setForeground(Color.white);
            labRank.setFont(UIUtils.TEXT_FONT991);
            this.add(labRank);
        }
        return labRank;
    }

    public void setLabRank(JLabel labRank) {
        this.labRank = labRank;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }


    //倒計時
    private void time3() {
        Runnable task = new Runnable() {
            private int count = 6;

            @Override
            public void run() {
                count--;
                switch (count) {
                    case 5:
                        image3 = CutButtonImage.getImage("inkImg/old/number/5.png", 42, 42);
                        break;
                    case 4:
                        image3 = CutButtonImage.getImage("inkImg/old/number/4.png", 42, 42);
                        break;
                    case 3:
                        image3 = CutButtonImage.getImage("inkImg/old/number/3.png", 42, 42);
                        break;
                    case 2:
                        image3 = CutButtonImage.getImage("inkImg/old/number/2.png", 42, 42);
                        break;
                    case 1:
                        image3 = CutButtonImage.getImage("inkImg/old/number/1.png", 42, 42);
                        break;
                    case 0:
                        image3 = CutButtonImage.getImage("inkImg/old/number/go.png", 42, 42);
                        btnLottey.goCJ();
                        executorService.shutdown();
                        break;
                }
            }
        };

        executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
     }
}
